﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ComponentModel;
using System.Reflection;

namespace AGVController.BaseClass
{
    public static class EnumHelper
    {
        /// <summary>
        /// 获取枚举值的描述信息（通过 DescriptionAttribute）
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="value">枚举值</param>
        /// <returns>描述文本（若未定义 DescriptionAttribute 则返回枚举名称）</returns>
        public static string GetEnumDescription<T>(T value) where T : Enum
        {
            // 获取枚举类型和字段名
            Type enumType = value.GetType();
            string name = Enum.GetName(enumType, value);

            if (string.IsNullOrEmpty(name))
            {
                return value.ToString();
            }

            // 获取字段的 DescriptionAttribute
            FieldInfo field = enumType.GetField(name);
            DescriptionAttribute attribute = field.GetCustomAttribute<DescriptionAttribute>(false);

            // 返回描述或字段名
            return attribute?.Description ?? name;
        }

        public static string Get_task_status_Description(double val)
        {
            string strDesc = "";
            switch(val)
            {
                case (int)task_status.NONE:
                    strDesc = GetEnumDescription(task_status.NONE);
                    break;
                case (int)task_status.CANCELED:
                    strDesc = GetEnumDescription(task_status.CANCELED);
                    break;
                case (int)task_status.COMPLETED:
                    strDesc = GetEnumDescription(task_status.COMPLETED);
                    break;
                case (int)task_status.FAILED:
                    strDesc = GetEnumDescription(task_status.FAILED);
                    break;
                case (int)task_status.RUNNING:
                    strDesc = GetEnumDescription(task_status.RUNNING);
                    break;
                case (int)task_status.SUSPENDED:
                    strDesc = GetEnumDescription(task_status.SUSPENDED);
                    break;
                case (int)task_status.WAITING:
                    strDesc = GetEnumDescription(task_status.WAITING);
                    break;
            }
            return strDesc;
        }

        public static string Get_task_type_Description(double val)
        {
            string strDesc = "";
            switch (val)
            {
                case (int)task_type.None:
                    strDesc = GetEnumDescription(task_type.None);
                    break;
                case (int)task_type.FreeNaviToAnyPoint:
                    strDesc = GetEnumDescription(task_type.FreeNaviToAnyPoint);
                    break;
                case (int)task_type.FreeNaviToId:
                    strDesc = GetEnumDescription(task_type.FreeNaviToId);
                    break;
                case (int)task_type.NaviToId:
                    strDesc = GetEnumDescription(task_type.NaviToId);
                    break;
                case (int)task_type.Other:
                    strDesc = GetEnumDescription(task_type.Other);
                    break;
                case (int)task_type.TranRota:
                    strDesc = GetEnumDescription(task_type.TranRota);
                    break;
            }
            return strDesc;
        }

        public static string Get_reloc_status_Description(double val)
        {
            string strDesc = "";
            switch (val)
            {
                case (int)reloc_status.COMPLETED:
                    strDesc = GetEnumDescription(reloc_status.COMPLETED);
                    break;
                case (int)reloc_status.FAILED:
                    strDesc = GetEnumDescription(reloc_status.FAILED);
                    break;
                case (int)reloc_status.RELOCING:
                    strDesc = GetEnumDescription(reloc_status.RELOCING);
                    break;
                case (int)reloc_status.SUCCESS:
                    strDesc = GetEnumDescription(reloc_status.SUCCESS);
                    break;
            }
            return strDesc;
        }

    }
}

